package com.hdaccp.user.controller;

import com.hdaccp.pojo.po.Admin;
import com.hdaccp.user.dao.AdminDao;
import com.hdaccp.user.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private AdminService adminService;
    @RequestMapping("/query")
    public List<Admin> query(){
        return adminDao.findAll();
    }
    @RequestMapping("/queryPage/{cpage}/{pagesize}")
    public List<Admin>queryPage(@PathVariable("cpage") int cpage, @PathVariable("pagesize") int pagesize){
        return adminService.queryPage(cpage,pagesize);
    }
    @RequestMapping("/save/{aname}/{apwd}/{ajibie}")
    public String save(@PathVariable("aname") String aname,@PathVariable("apwd")String apwd,@PathVariable("ajibie")String ajibie){
       String msg="添加成功 ~";
        Admin admin=new Admin();
        admin.setAname(aname);
        admin.setApwd(apwd);
        admin.setAjibie(ajibie);

        try {
            adminDao.save(admin);
        } catch (Exception e) {
            msg="添加失败";
        }
        return msg;
    }
    @RequestMapping("/update/{id}/{aname}/{apwd}/{ajibie}")
    public String update(@PathVariable("id") int aid,@PathVariable("aname") String aname,@PathVariable("apwd")String apwd,@PathVariable("ajibie")String ajibie){
        String msg="修改成功 ~";
        Admin admin=adminDao.findById(aid).get();
        admin.setAname(aname);
        admin.setApwd(apwd);
        admin.setAjibie(ajibie);

        try {
            adminDao.save(admin);
        } catch (Exception e) {
            msg="修改失败";
        }
        return msg;
    }
    @RequestMapping("/delete/{aid}")
    public String delete(@PathVariable("aid") int aid){
        String msg="删除成功~";
        try{
            adminDao.deleteById(aid);
        }catch (Exception e){
            msg="删除失败！";
        }
        return msg;
    }
}
